package Convertisseur;

import graphEd.Infos;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;
import DataStorage.CritereDAOHibernate;
import DataStorage.CritereTextDAOHibernate;
import DataStorage.InfluenceDAOHibernate;
import DataStorage.InformationLigneDAOHibernate;
import DataStorage.TNC_ObjectifDAOHibernate;
import FileSystemView.FileOperate;
import HibernateSQLite.java.model.Critere;
import HibernateSQLite.java.model.CritereText;
import HibernateSQLite.java.model.InformationLigne;

//public class RecepCharacteristicByDB extends VariableFlags{
public class RecepCharacteristicByDB {

	private FileOperate file = new FileOperate();
	private String fileName_dfq = "";

	private CritereDAOHibernate critereDAO = new CritereDAOHibernate();
	private CritereTextDAOHibernate critereTextDAO = new CritereTextDAOHibernate();
	private InfluenceDAOHibernate influenceDAO = new InfluenceDAOHibernate();
	private InformationLigneDAOHibernate informationLigneDAO = new InformationLigneDAOHibernate();
	private long InformationLigne_id;
	private Workbook book = null;
	private Sheet sheet = null;
	private String critere_name;
	private String Organe;
	private String Diversite;
	private String Synoptique_Ligne = "";
	private String AdresseImageRoot = "";
	private String Error_Message = "";
	private String Commentaire_Echantillon_Organe = "";
	private String site;
	private String N_ligne;
	private String Annee;

	public String getSynoptique_Ligne() {
		return Synoptique_Ligne;
	}

	public void setSynoptique_Ligne(String synoptique_Ligne) {
		Synoptique_Ligne = synoptique_Ligne;
	}

	public String getAnnee() {
		return Annee;
	}

	public void setAnnee(String annee) {
		Annee = annee;
	}

	public String getSite() {
		return site;
	}

	public void setSite(String site) {
		this.site = site;
	}

	public String getN_ligne() {
		return N_ligne;
	}

	public void setN_ligne(String n_ligne) {
		N_ligne = n_ligne;
	}

	public long getInformationLigne_id() {
		return InformationLigne_id;
	}

	public void setInformationLigne_id(long informationLigne_id) {
		InformationLigne_id = informationLigne_id;
	}

	public String getFileName_dfq() {
		return fileName_dfq;
	}

	public void setFileName_dfq(String fileName_dfq) {
		this.fileName_dfq = fileName_dfq;
	}

	@SuppressWarnings("unused")
	public String getError_Message(String coupe, String OP) {
		Error_Message = "";
		//verifier Meta Regles
		//VerifierMetaReglesDeCoherenceEchantillon verifierMetaRegles = new VerifierMetaReglesDeCoherenceEchantillon();
		Map<String, String> mapMetaRegles = VerifierMetaReglesDeCoherenceEchantillon
				.getMap_OP_message();
		if (mapMetaRegles.containsKey(OP)) {
			Error_Message = mapMetaRegles.get(OP);
		}
		//verifier les photo
		Infos infos = Infos.getInstance();
		Map<String, String> mapCoupe_message = infos.getMessagesPhotos();
		if (mapCoupe_message.containsKey(coupe)) {
			Error_Message = Error_Message + mapCoupe_message.get(coupe);
		}
		//verifier les list coupe
		VerifierListCoupeEchantillonAvecListDefinie verifierListCoupe = new VerifierListCoupeEchantillonAvecListDefinie();
		Map<String, String> mapCoupe = VerifierListCoupeEchantillonAvecListDefinie
				.getMap_coupe_message();
		if (mapCoupe.containsKey(coupe)) {
			Error_Message = Error_Message + mapCoupe.get(coupe);
		}
		return Error_Message;
	}

	public void setError_Message(String error_Message) {
		Error_Message = error_Message;
	}

	public String getAdresseImageRoot() {
		return AdresseImageRoot;
	}

	public void setAdresseImageRoot(String adresseImageRoot) {
		AdresseImageRoot = adresseImageRoot;
	}

	public String getCommentaire_Echantillon_Organe() {
		Commentaire_Echantillon_Organe = "";
		@SuppressWarnings("unused")
		VerifierCheminsEchantillon verifierChemins = new VerifierCheminsEchantillon();
		Map<String, String> mapMessageChemin = VerifierCheminsEchantillon
				.getMap_essieu_messageChemin();
		for (Object m : mapMessageChemin.keySet()) {
			Commentaire_Echantillon_Organe = Commentaire_Echantillon_Organe
					+ mapMessageChemin.get(m);
		}
		return Commentaire_Echantillon_Organe;
	}

	public void setCommentaire_Echantillon_Organe(
			String commentaire_Echantillon_Organe) {
		Commentaire_Echantillon_Organe = commentaire_Echantillon_Organe;
	}

	public Workbook getBook() {
		return book;
	}

	public void setBook(Workbook book) {
		this.book = book;
	}

	public void init(String filePath) {
		File fileExcel = new File(filePath);
		try {
			// obtain file Excel
			book = Workbook.getWorkbook(fileExcel);
			// bookCompare = Workbook.getWorkbook(fileCompareTable);

			sheet = book.getSheet(0);

			Execute();
			System.out.println("Fin de execute ecrite characteristic ");
			// --------------------------
			// Fin Execute
			// --------------------------
			book.close();
			// bookCompare.close();
		} catch (BiffException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public void Execute() {
		int indexEchantillon = 1;
		// this.getTypeEssieu(sheet);
		this.getCritere_nameByMacro(sheet);
		long critere_id = critereDAO.getCritere_id(critere_name);

		System.out.println("critere_name : " + critere_name);
		System.out.println("Organe : " + Organe);
		System.out.println("Diversite : " + Diversite);
		System.out.println("site : " + site);
		System.out.println("N_ligne : " + N_ligne);
		System.out.println("Annee : " + Annee);
		System.out.println("critere_id:" + critere_id);

		InformationLigne_id = informationLigneDAO.getInformationLigne_ID(
				Organe, Diversite, site, N_ligne, Annee);

		System.out.println("InformationLigne_id: " + InformationLigne_id);

		// critereTextDAO = new CritereTextDAOHibernate();
		System.out.println("NbEchantillon" + this.getNbEchantillon());

		file.newFile(
				fileName_dfq,
				"K0100 " + this.getNbEchantillon() + "\r\n"
						+ "K1001/"
						+ indexEchantillon
						+ " "
						+ this.getOrgane()
						+ "\r\n" // ok
						+ "K1005/"
						+ indexEchantillon
						+ " "
						+ this.getDiversite()
						+ "\r\n" // ok
						+ "K1046/" + indexEchantillon
						+ " "
						+ this.getSynoptique_Ligne()
						+ "\r\n" // ??
						+ "K1091/" + indexEchantillon + " "
						+ this.getNumero_de_Ligne()
						+ "\r\n" // ok
						+ "K1900/" + indexEchantillon + " "
						+ this.getCommentaire_Echantillon_Organe() + "\r\n" // ??
		);

		// for (CritereText critereText :
		// critereTextDAO.getListCritereText(critere_id)) {
		for (CritereText critereText : critereTextDAO
				.getListCritereText(critere_name)) {
			String N_cordon = critereText.getN_cordon();
			List<String> listCoupeByCordon = influenceDAO.getListVueByN_cordon(
					N_cordon, InformationLigne_id);
			List<String> listCarc = critereTextDAO
					.getListValidtyByL1_L2_P1_P2_A_B(critereText);

			for (String coupe : listCoupeByCordon) {
				for (String carc : listCarc) {
					// file.newFile(fileName_dfq, "");
					if (carc.contentEquals("b")) {
						String Upper_Specification_Limit = this
								.getUpper_Specification_Limit(critereText);
						file.newFile(fileName_dfq,"K2001/"+ indexEchantillon+ " "+ coupe+ "\r\n" // ok
								
							+ "K2002/"+ indexEchantillon+ " "+ carc+ "\r\n" // ok
							+ "K2003/"+ indexEchantillon+ " "+ critereText.getDESIGNATION()+ "\r\n" 
							+ "K2004/"+ indexEchantillon+ " "+ getCharacteristic_Type()+ "\r\n" 
							+ "K2005/"+ indexEchantillon+ " "+ getTNC_Objectif_Qdas(critereText)+ "\r\n"
							+ "K2009/"+ indexEchantillon+ " "+ getRemarque_sur_Coupe()+ "\r\n" // ok
							+ "K2022/"+ indexEchantillon+ " "+ getDecimal_Places(Upper_Specification_Limit)+ "\r\n"
							+ "K2111/"+ indexEchantillon+ " "+ Upper_Specification_Limit+ "\r\n" // ok
							+ "K2120/"+ indexEchantillon+ " "+ "0"+ "\r\n"
							+ "K2121/"+ indexEchantillon+ " "+ "1"+ "\r\n"
							+ "K2142/"+ indexEchantillon+ " "+ getUnit()+ "\r\n"
							+ "K2243/"+ indexEchantillon+ " "+ getAdresse_Image_Macrographie(coupe)+ "\r\n"
							+ "K2301/"+ indexEchantillon+ " "+ this.getType_Machine(critereText)+ "\r\n" // ok
							+ "K2311/"+ indexEchantillon+ " "+ critereText.getCLASSE()+ "\r\n" // ok
							+ "K2312/"+ indexEchantillon+ " "+ critereText.getOP()+ "\r\n" // ok
							+ "K2900/"+ indexEchantillon+ " "+getError_Message(coupe,critereText.getOP()) + "\r\n"
						);
					} else {
						String Lower_Specification_Limit = this
								.getLower_Specification_Limit(critereText, carc);
						file.newFile(
								fileName_dfq,
								"K2001/"
										+ indexEchantillon
										+ " "
										+ coupe
										+ "\r\n" // ok
										+ "K2002/"
										+ indexEchantillon
										+ " "
										+ carc
										+ "\r\n" // ok
										+ "K2003/"
										+ indexEchantillon
										+ " "
										+ critereText.getDESIGNATION()
										+ "\r\n" // ok
										+ "K2004/"
										+ indexEchantillon
										+ " "
										+ this.getCharacteristic_Type()
										+ "\r\n" // ok
										+ "K2005/"
										+ indexEchantillon
										+ " "
										+ this.getTNC_Objectif_Qdas(critereText)
										+ "\r\n" // ok
										+ "K2009/"
										+ indexEchantillon
										+ " "
										+ this.getRemarque_sur_Coupe()
										+ "\r\n" // ok
										+ "K2022/"
										+ indexEchantillon
										+ " "
										+ this.getDecimal_Places(Lower_Specification_Limit)
										+ "\r\n" // ok
										+ "K2110/"
										+ indexEchantillon
										+ " "
										+ Lower_Specification_Limit
										+ "\r\n" // ok
										+ "K2120/"
										+ indexEchantillon
										+ " "
										+ "1"
										+ "\r\n" // ok
										+ "K2121/"
										+ indexEchantillon
										+ " "
										+ "0"
										+ "\r\n" // ok
										+ "K2142/"
										+ indexEchantillon
										+ " "
										+ this.getUnit()
										+ "\r\n" // ok
										+ "K2243/"
										+ indexEchantillon
										+ " "
										+ this.getAdresse_Image_Macrographie(coupe)
										+ "\r\n" // ??
										+ "K2301/"
										+ indexEchantillon
										+ " "
										+ this.getType_Machine(critereText)
										+ "\r\n" // ok
										+ "K2311/"
										+ indexEchantillon
										+ " "
										+ critereText.getCLASSE()
										+ "\r\n" // ok
										+ "K2312/"
										+ indexEchantillon
										+ " "
										+ critereText.getOP()
										+ "\r\n" // ok
										+ "K2900/"
										+ indexEchantillon
										+ " "
										+ this.getError_Message(coupe,
												critereText.getOP()) + "\r\n" // ??
						);
					}

					file.newFile(fileName_dfq, "\r\n");
					indexEchantillon++;
				}
			}

		}
	}

	public String getCritere_nameByMacro(Sheet sheet) {
		// reception values cells
		Cell cellK1001 = sheet.getCell("A1"); // Organe(nom_Essieu):X91_Essieu

		String piece = cellK1001.getContents().toString();
		critere_name = piece.substring(piece.lastIndexOf('\\') + 1,
				piece.lastIndexOf(".pce"));
		return critere_name;
	}

	public String getCritere_name() {
		return critere_name;
	}

	// K1001
	public String getOrgane() {
		return Organe;
	}

	// K1005
	public String getDiversite() {
		return Diversite;
	}

	public void setCritere_name(String critere_name) {
		this.critere_name = critere_name;
	}

	public void setOrgane(String organe) {
		Organe = organe;
	}

	public void setDiversite(String diversite) {
		Diversite = diversite;
	}

	// K1091
	public String getNumero_de_Ligne() {
		long InformationLigne_id = informationLigneDAO.getInformationLigne_ID(
				Organe, Diversite, site, N_ligne, Annee);
		InformationLigne informationLigne = informationLigneDAO
				.getInformationLigne(InformationLigne_id);
		return informationLigne.getN_ligne();
	}

	// K0100
	@SuppressWarnings("unused")
	public int getNbEchantillon() {
		int nbEchantillon = 0;

		// this.getTypeEssieu(sheet);
		this.getCritere_nameByMacro(sheet);
		long critere_id = critereDAO.getCritere_id(critere_name);
		long InformationLigne_id = informationLigneDAO.getInformationLigne_ID(
				Organe, Diversite, site, N_ligne, Annee);
	
		// critereTextDAO = new CritereTextDAOHibernate();
		int nbEchantillonByCordon = 0;
		Critere critere = critereDAO.getCritere(critere_id);
		List<String> listTemp = new ArrayList<String>();
		// for (CritereText critereText :
		// critereTextDAO.getListCritereText(critere_id)) {
		// for (CritereText critereText :
		// critereTextDAO.getListCritereText(critere)) {
		for (CritereText critereText : critereTextDAO
				.getListCritereText(critere_name)) {
			String N_cordon = critereText.getN_cordon();

			listTemp.add(N_cordon);

			int nbVueByCordon = influenceDAO.getNumberVueByN_cordon(N_cordon,
					InformationLigne_id);
			int nbCharc = critereTextDAO
					.getValidtyNumberByL1_L2_P1_P2_A_B(critereText);
			nbEchantillonByCordon = nbVueByCordon * nbCharc;
			nbEchantillon = nbEchantillon + nbEchantillonByCordon;
		}
		return nbEchantillon;
	}

	// K2004 fix=0
	public String getCharacteristic_Type() {
		return "0";
	}

	// K2005
	public String getTNC_Objectif_Qdas(CritereText critereText) {
		String CLASSE = critereText.getCLASSE();
		TNC_ObjectifDAOHibernate tnc_objectifDAO = new TNC_ObjectifDAOHibernate();
		String tnc_objectif_Qdas = tnc_objectifDAO.getTNC_Objectif_Qdas(CLASSE);
		return tnc_objectif_Qdas;
	}

	// K2009
	public String getRemarque_sur_Coupe() {
		return "200";
	}

	// K2022
	public String getDecimal_Places(String str) {
		return "" + ((str.length()) - (str.indexOf(".")) - 1);
	}

	// K2111
	public String getUpper_Specification_Limit(CritereText critereText) {
		return critereText.getB();
	}

	// K2110
	public String getLower_Specification_Limit(CritereText critereText,
			String carc) {
		String Lower_Specification_Limit = "";
		if (carc.contentEquals("L1")) {
			Lower_Specification_Limit = critereText.getL1();
		}
		if (carc.contentEquals("L2")) {
			Lower_Specification_Limit = critereText.getL2();
		}
		if (carc.contentEquals("P1")) {
			Lower_Specification_Limit = critereText.getP1();
		}
		if (carc.contentEquals("P2")) {
			Lower_Specification_Limit = critereText.getP2();
		}
		if (carc.contentEquals("a")) {
			Lower_Specification_Limit = critereText.getA();
		}
		if (carc.contentEquals("b")) {
			Lower_Specification_Limit = critereText.getB();
		}
		return Lower_Specification_Limit;
	}

	// K2142
	public String getUnit() {
		return "mm";
	}

	// K2243
	public String getAdresse_Image_Macrographie(String coupe) {
		return AdresseImageRoot + coupe + ".jpg";
	}

	// K2301
	public String getType_Machine(CritereText critereText) {
		String Type_Machine = "";
		String CONFIG = critereText.getCONFIG();
		if (CONFIG.equals("I")) {
			Type_Machine = "1";
		}
		if (CONFIG.equals("II")) {
			Type_Machine = "2";
		}
		if (CONFIG.equals("III")) {
			Type_Machine = "3";
		}
		if (CONFIG.equals("S")) {
			Type_Machine = "4";
		}
		if (!CONFIG.equals("I") && !CONFIG.equals("II")
				&& !CONFIG.equals("III") && !CONFIG.equals("S")) {
			Type_Machine = CONFIG;
		}
		return Type_Machine;
	}

	public static void main(String[] args) {
		RecepCharacteristicByDB r = new RecepCharacteristicByDB();
		// r.getNbEchantillon();
		// System.out.println(r.getNbEchantillon());
		r.setSite("Le Mans");
		r.setN_ligne("1");
		r.setAnnee("2012");
		r.setFileName_dfq("file2.dfq");

		r.init("temp\\x91-2rd.xls");
		// r.init("temp\\X95_Types 1,2,A,B,C.xls");
		// System.out.println(r.getNbEchantillon());

	}
}
